package com.hu.web.controller.business;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hu.common.annotation.Log;
import com.hu.common.core.controller.BaseController;
import com.hu.common.core.domain.AjaxResult;
import com.hu.common.enums.BusinessType;
import com.hu.system.domain.ProfitStatistics;
import com.hu.system.service.IProfitStatisticsService;
import com.hu.common.utils.poi.ExcelUtil;
import com.hu.common.core.page.TableDataInfo;

/**
 * 利润统计Controller
 *
 * @author hu
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/business/profit/statistics")
public class ProfitStatisticsController extends BaseController {
    @Autowired
    private IProfitStatisticsService profitStatisticsService;

    /**
     * 查询利润统计列表
     */
    @PreAuthorize("@ss.hasPermi('profit:statistics:list')")
    @GetMapping("/list")
    public AjaxResult list(ProfitStatistics profitStatistics) {
        startPage();
        List<ProfitStatistics> list = profitStatisticsService.selectProfitStatisticsList(profitStatistics);
        TableDataInfo dataTable = getDataTable(list);
        
        // 获取汇总统计数据（基于全部符合条件的数据，而不是分页数据）
        Map<String, Object> summary = profitStatisticsService.selectProfitSummary(profitStatistics);
        
        // 使用 AjaxResult 返回，包含分页数据和统计数据
        AjaxResult result = AjaxResult.success();
        result.put("rows", dataTable.getRows());
        result.put("total", dataTable.getTotal());
        result.put("summary", summary);
        
        return result;
    }

    /**
     * 导出利润统计列表
     */
    @PreAuthorize("@ss.hasPermi('profit:statistics:export')")
    @Log(title = "利润统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProfitStatistics profitStatistics) {
        List<ProfitStatistics> list = profitStatisticsService.selectProfitStatisticsList(profitStatistics);
        ExcelUtil<ProfitStatistics> util = new ExcelUtil<ProfitStatistics>(ProfitStatistics.class);
        util.exportExcel(response, list, "利润统计数据");
    }
}

